/**
 * Created by lvzj on 15/10/14.
 */

init();

var type = params.type;

function init() {

    var et = params.et;

    var bt = (parseInt(et) - (60 * 60 * 24 * 7));

    $.ajax({
        type: "GET",
        url: serverUrl + '/rest/v1.0/communities/getWeekReport/' + params.token + '/' + params.deviceId + '?bt=' + bt + '&et=' + et,
        data: {},
        dataType: "json",
        success: function (data) {

            if (data.data == "") {
                $('.loading-pop').html('<font color="black">没有查询到相关内容</font>');
                return;
            }

            var hotList = data.data.report.hotlist;

            var str = "";
            for (var i = 0; i < hotList.length; i++) {
                str += '<div class="week-rank week-second">';
                str += '<p class="week-t">NO.' + (i + 1) + '</p>';
                str += '<a href="javascript:toQuestionDetail(\'' + hotList[i].question.objId + '\',\'' + hotList[i].question.createdBy.objId + '\',\'' + hotList[i].question.category + '\')">';
                str += '<p class="detail-p">' + hotList[i].question.desc + '</p>';
                str += '<div class="detail-w">';
                str += '<div class="userinfo">';
                str += '<img src="' + hotList[i].question.headImg + '" onclick="event.cancelBubble=true;toPeronalPage(\'' + hotList[i].question.createdBy.objId + '\')" class="avatar">';
                str += '<p class="info-p"><span class="name">' + hotList[i].question.createdBy.nickName + '</span><br/><span class="info-sub">' + formatTime(hotList[i].question.dateCreated) + '&nbsp;' + hotList[i].question.community.communityName + '</span></p>';
                str += '</div>';
                str += '<div class="num"><img src="' + resUrl + 'images/week/visiter.png">' + hotList[i].browserCount + '<img src="' + resUrl + 'images/week/comment.png">' + hotList[i].replyCount + '</div>';
                str += '</div>';
                str += '</a>';
                if (hotList[i].question.bestReply != null) {
                    str += '<img src="' + hotList[i].question.bestReply.headImg + '" onclick="toPeronalPage(\'' + hotList[i].question.bestReply.repliedBy.objId + '\')" class="avatar-commenter">' + hotList[i].question.bestReply.repliedBy.nickName + '的精彩回复：';
                    str += '<p>' + hotList[i].question.bestReply.replyDesc + '</p>';
                }
                str += '</div>';

            }

            $('#questionList').html(str);

            if (data.data.report.contribution != null && data.data.report.contribution.length > 0) {
                str = "";
                for (var i = 0; i < data.data.report.contribution.length; i++) {
                    if (i == 0) {
                        $('.one-p').html('恭喜' + data.data.report.contribution[i].uid.nickName + '<br/>' + '夺得本周冠军');
                        $('#headImg').attr('src', data.data.report.contribution[i].headImg).attr('onclick', 'toPeronalPage(\'' + data.data.report.contribution[i].uid.objId + '\')');
                    } else {
                        str += "<li>";
                        str += '<div><img src="' + data.data.report.contribution[i].headImg + '" onclick="toPeronalPage(\'' + data.data.report.contribution[i].uid.objId + '\')" class="avatar"><span class="rank-num rank-2">' + (i + 1) + '</span></div>';
                        str += data.data.report.contribution[i].uid.nickName;
                        str += "</li>";
                    }
                }
                $('#contributionList').html(str);
            } else {
                $('#week-four').hide();
            }

            if (data.data.report.new_neighbors != null && data.data.report.new_neighbors.length > 0) {
                str = "";
                for (var i = 0; i < data.data.report.new_neighbors.length; i++) {
                    if (i >= 12) {
                        break;
                    }
                    str += "<li>";
                    if (data.data.report.new_neighbors[i].uid.gender == '女')
                        str += '<span><img src="' + data.data.report.new_neighbors[i].headImg + '" onclick="toPeronalPage(\'' + data.data.report.new_neighbors[i].uid.objId + '\')" class="avatar"><img src="' + resUrl + 'images/week/woman.png" class="sex sex-woman"></span>';
                    else
                        str += '<span><img src="' + data.data.report.new_neighbors[i].headImg + '" onclick="toPeronalPage(\'' + data.data.report.new_neighbors[i].uid.objId + '\')" class="avatar"><img src="' + resUrl + '/images/week/man.png" class="sex sex-woman"></span>';
                    str += '<div>' + data.data.report.new_neighbors[i].uid.nickName + '</div>';
                    str += "</li>";
                }

                $('#newList').html(str);
            } else {
                $('.week-newlj-t').hide();
                $('.week-newlj').hide();
            }

            var score = data.data.score;

            var addScore = 0;
            var adopt = 0;
            var thank = 0;
            var praise = 0;

            if (score != null) {
                addScore = score.addRecord == null ? 0 : score.addRecord;
                adopt = score.adopt == null ? 0 : score.adopt;
                thank = score.thank == null ? 0 : score.thank;
                praise = score.praise == null ? 0 : score.praise;
            }

            var totalScore = (parseInt(adopt) * 5) + (parseInt(thank) * 2) + (parseInt(praise) * 1);

            var endStr = "";

            if (totalScore < 20) {
                endStr = "本周发生了很多有趣的事，你都没有参与，好遗憾";
            } else if (totalScore >= 20 && totalScore <= 45) {
                endStr = "表现的还可以，还可以再加把劲";
            } else {
                endStr = "亲爱的，你真是太赞了，继续加油";
            }

            $('#end').html(endStr);

            /*$('.my-jifen').html('获得了' + addScore + '个积分');*/
            $('#adopt').html(adopt);
            $('#thank').html(thank);
            $('#praise').html(praise);

            $('.share-a').attr('onclick', 'toInvitePage(\'' + data.data.score.userId + '\')');

            $('.loading-pop').hide();
        }
    });
}


function toPeronalPage(uid) {
    if (type == 'ios') {
        document.location.href = "66xiaoqu:toPeronalPage##userId=" + uid;
    } else {
        webviewhandle.goToPeronalPage(uid);//webviewhandle为webview注册对象
    }
};
function toInvitePage(uid) {
    if (type == 'ios') {
        document.location.href = "66xiaoqu:toInvitePage##userId=" + uid;
    } else {
        webviewhandle.goToInvitePage(uid);//webviewhandle为webview注册对象
    }
};
function toQuestionDetail(questionId, createdBy, category) {
    if (type == 'ios') {
        document.location.href = "66xiaoqu:toQuestionDetail##objId=" + questionId + "&nbsp;createdBy=" + createdBy;
        +"&nbsp;category=" + category;
    } else {
        webviewhandle.goToQuestionDetail(questionId, createdBy, category);//webviewhandle为webview注册对象
    }
};

function formatTime(input) {

    var d = new Date(input), now = new Date(),
        dStamp = d.getTime(), nowStamp = now.getTime();
    if (now.getFullYear() - d.getFullYear() > 0) {
        return d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
    } else if ((nowStamp - dStamp) / 1000 < 60) {
        return "刚刚";
    } else if ((nowStamp - dStamp) / 60000 < 60) {
        return Math.floor((nowStamp - dStamp) / 60000) + "分钟前";
    } else if ((nowStamp - dStamp) / 3600000 < 24) {
        return Math.floor((nowStamp - dStamp) / 3600000) + "小时前";
    } else if ((nowStamp - dStamp) / 86400000 >= 1 && (nowStamp - dStamp) / 86400000 <= 3) {
        return Math.floor((nowStamp - dStamp) / 86400000) + "天前";
    } else if ((nowStamp - dStamp) / 86400000 > 3) {
        return (d.getMonth() + 1) + "月" + d.getDate() + "日";
    }
};